package bc.swim2.ejb.entity;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@NamedQueries({
		@NamedQuery(name = "RegisteredUser.getRegisteredUserByUsername", query = "SELECT ru FROM RegisteredUser ru WHERE ru.username = :username"),
		@NamedQuery(name = "RegisteredUser.getRegisteredUserByUsernamePassword", query = "SELECT ru FROM RegisteredUser ru WHERE ru.username = :username AND ru.password = :password"),
		@NamedQuery(name = "RegisteredUser.getRegisteredUserByPattern", query = "SELECT DISTINCT ru FROM RegisteredUser ru WHERE (CONCAT(ru.firstName, CONCAT(' ', ru.lastName)) LIKE :pattern or CONCAT(ru.lastName, CONCAT(' ', ru.firstName)) LIKE :pattern)"),
		@NamedQuery(name = "RegisteredUser.getRegisteredUserByAbilityId", query = "SELECT ru FROM RegisteredUser ru, Ability a WHERE a.id =:id and a MEMBER OF ru.abilityPossessed"),
		@NamedQuery(name = "RegisteredUser.getRegisteredUserById", query = "SELECT ru FROM RegisteredUser ru WHERE ru.id = :id"),
		@NamedQuery(name = "RegisteredUser.getRegisteredUserByEmail", query = "SELECT ru FROM RegisteredUser ru WHERE ru.email = :email") })
@Entity
@Table(name = "registered_user")
public class RegisteredUser implements Serializable {

	/**
	 * Default constructor
	 */
	public RegisteredUser() {
	}

	/**
	 * Constructor with all the parameters
	 * 
	 * @param id
	 * @param username
	 * @param password
	 * @param firstname
	 * @param lastname
	 * @param email
	 * @param birthdate
	 */
	public RegisteredUser(int id, String username, String password,
			String firstname, String lastname, String email, Date birthdate) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
		this.firstName = firstname;
		this.lastName = lastname;
		this.email = email;
		this.birthdate = birthdate;
	}

	private static final long serialVersionUID = 931638401988771053L;

	@Id
	@GeneratedValue
	@Column(name = "Id", nullable = false)
	private int id;

	@Column(name = "Username", nullable = false)
	private String username;

	@Column(name = "Password", nullable = false)
	private String password;

	@Column(name = "FirstName", nullable = false)
	private String firstName;

	@Column(name = "LastName", nullable = false)
	private String lastName;

	@Column(name = "Email", nullable = false)
	private String email;

	@Column(name = "BirthDate", nullable = false)
	private java.util.Date birthdate;

	/**
	 * Relationship between User and Ability creates a jointable called Belongs
	 */
	@ManyToMany
	@JoinTable(name = "Belongs", joinColumns = { @JoinColumn(name = "username", referencedColumnName = "username") }, inverseJoinColumns = { @JoinColumn(name = "name_ability", referencedColumnName = "name") })
	public List<Ability> abilityPossessed;

	/**
	 * Getter method for Id attribute
	 * 
	 * @return id
	 */
	public int getId() {
		return id;
	}

	/**
	 * Setter method for Id attribute
	 * 
	 * @param id
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * Getter method for username attribute
	 * 
	 * @return username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * Setter method for username attribute
	 * 
	 * @param username
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * Getter method for password attribute
	 * 
	 * @return password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * Setter method for password attribute
	 * 
	 * @param password
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * Getter method for firstname attribute
	 * 
	 * @return firstname
	 */
	public String getFirstName() {
		return firstName;
	}

	/**
	 * Setter method for firstname attribute
	 * 
	 * @param firstname
	 */
	public void setFirstName(String firstname) {
		this.firstName = firstname;
	}

	/**
	 * Getter method for lastname attribute
	 * 
	 * @return lastname
	 */
	public String getLastName() {
		return lastName;
	}

	/**
	 * Setter method for lastname attribute
	 * 
	 * @param lastname
	 */
	public void setLastName(String lastname) {
		this.lastName = lastname;
	}

	/**
	 * Getter method for email attribute
	 * 
	 * @return email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * Setter method for email attribute
	 * 
	 * @param email
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * Getter method for birthdate attribute
	 * 
	 * @return birthdate
	 */
	public Date getBirthDate() {
		return birthdate;
	}

	/**
	 * Setter method for birthdate attribute
	 * 
	 * @param birthdate
	 */
	public void setBirthDate(String birthdate) {
		try {
			DateFormat df = new SimpleDateFormat("dd-mm-yyyy");
			Date bd;
			bd = (Date) df.parse(birthdate);
			this.birthdate = bd;
		} catch (ParseException e) {
			System.out.println("Exception :" + e);
		}

	}

	/**
	 * Getter method for abilityPossessed attribute
	 * 
	 * @return abilityPossessed
	 */
	public List<Ability> getAbilityPossessed() {
		return abilityPossessed;
	}

	/**
	 * Setter method for the list of abilities possessed by an user
	 * 
	 * @param abilityPossessed
	 */
	public void setAbilityPossessed(List<Ability> abilityPossessed) {
		this.abilityPossessed = abilityPossessed;
	}

}
